﻿#region 信息

// Copyright©2014 Rex Rao all rights reserved.
// File name : Mojito\Mojito.Service\ShopConfig.cs
// Author : Sohobloo
// Create time : 2014/09/02 12:17
// Update time : 2014/09/03 17:17

#endregion

#region 引用

using System;
using System.ComponentModel.Composition;
using System.Linq;
using Mojito.Data.Repository;
using Sohobloo.SoCommon;
using Sohobloo.SoMcCoffee.Service;

#endregion

namespace Mojito.Service.Impl
{
    [Export(typeof(IShopConfig))]
    public class ShopConfig : ServiceBase, IShopConfig
    {
        [Import]
        public IShopRepository ShopRepository { get; set; }

        [Import]
        public IAccountRepository AccountRepository { get; set; }

        public string ShopName
        {
            get { return ShopRepository.Entities.Select(s => s.ShopName).Single(); }
            set
            {
                var shop = ShopRepository.Entities.Single();
                shop.ShopName = value;
                ShopRepository.Update(shop);
                Commit();
            }
        }

        public TimeZoneInfo TimeZone
        {
            get
            {
                var timeZoneId = ShopRepository.Entities
                                               .Select(s => s.TimeZone)
                                               .Single();
                return TimeZoneInfo.FindSystemTimeZoneById(timeZoneId);
            }
            set
            {
                ArgumentHelper.Instance.CheckNull(value);
                var shop = ShopRepository.Entities.Single();
                shop.TimeZone = value.Id;
                ShopRepository.Update(shop);
                Commit();
            }
        }

        public TimeSpan DailySettlementTime
        {
            get
            {
                var dailySettlementTimeTicks = ShopRepository.Entities
                                                             .Select(s => s.DailySettlementTime)
                                                             .Single();
                return new TimeSpan(dailySettlementTimeTicks);
            }
            set
            {
                ArgumentHelper.Instance.CheckRange(value.TotalMinutes, 0, 24 * 60 - 1);
                var shop = ShopRepository.Entities.Single();
                shop.DailySettlementTime = value.Ticks;
                ShopRepository.Update(shop);
                Commit();
            }
        }

        public Guid DefaultAccountId
        {
            get { return ShopRepository.Entities.Select(s => s.DefaultAccountId).Single(); }
            set
            {
                var shop = ShopRepository.Entities.Single();
                shop.DefaultAccountId = value;
                ShopRepository.Update(shop);
                Commit();
            }
        }

        public bool ConfuseOrderNumber
        {
            get { return ShopRepository.Entities.Select(s => s.ConfuseOrderNumber).Single(); }
            set
            {
                var shop = ShopRepository.Entities.Single();
                shop.ConfuseOrderNumber = value;
                ShopRepository.Update(shop);
                Commit();
            }
        }
    }
}